package org.wx.herostory.services.util;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @description: redis 工具类
 * @author: wangxun
 * @createDate: 2021/3/5
 * @version: 1.0
 */
@Slf4j
public final class RedisUtil {
    /**
     * 单例
     */
    static private final RedisUtil _instance = new RedisUtil();

    static private JedisPool _jedisPool;

    private RedisUtil(){}

    static public RedisUtil getInstance(){
        return _instance;
    }

    static public void init(){
        try {
            _jedisPool = new JedisPool("test.wx.cn", 6379);
            log.info("Redis 启动成功");
        } catch (Exception e) {
            log.error("Redis 启动失败:{}",e.getMessage());
        }
    }

    /**
     * 获取 redis 操作对象
     * @return
     */
    static public Jedis getJedis(){
        if(null == _jedisPool){
            throw new RuntimeException("_jedisPool 未初始化");
        }
        Jedis jedis = _jedisPool.getResource();
        jedis.auth("123456");

        return jedis;
    }

}
